package com.springboot.mail.modules.User.Controller;


import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.springboot.mail.common.ResultUtil;
import com.springboot.mail.common.bean.MyResult;
import com.springboot.mail.modules.User.domain.entity.UserDO;
import com.springboot.mail.modules.User.domain.vo.LoginVO;
import com.springboot.mail.modules.User.domain.vo.ResponseLoginVO;
import com.springboot.mail.modules.User.domain.vo.UserVO;
import com.springboot.mail.modules.User.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "登录模块")
@ApiSupport(author = "LY L")
@RestController
@RequestMapping(value = "/user", produces = {"application/json;charset=UTF-8"})
@Slf4j
@Validated     // springboot 2.5.* 版本需要在类上标注 @Validated 注解校验注解才会生效。
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册", notes = "")
    @PostMapping("/register")
    public MyResult<Boolean> registerUser(@RequestBody @Validated UserVO user) {
        return userService.registerUser(user);
    }

    @ApiOperation(value = "登录", notes = "")
    @PostMapping("/login")
    public MyResult<ResponseLoginVO> loginUser(@RequestBody @Validated LoginVO loginVO) {
        return userService.loginUser(loginVO.getUsername(), loginVO.getPassword());
    }

    @ApiOperation(value = "登出", notes = "")
    @PostMapping("/logout")
    public MyResult<String> logout() {
        return userService.logout();
    }

    @ApiOperation("查询登录状态")
    @GetMapping("/isLogin")
    public MyResult<String> isLogin() {
        // StpUtil.isLogin() 查询当前客户端是否登录，返回 true 或 false
        return StpUtil.isLogin() ? ResultUtil.success("已登录。。") : ResultUtil.error("未登录！！");
    }

}